import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import { Toast, Bubble, Tools } from 'weaver-mobile';
import { MapPage } from 'weaver-mobile-page';
import * as adapter from '../../constants/adapter';
const getCurrentPosition = MapPage.getCurrentPosition;
const getLabel = Tools.getLabel;
export default class CustomerMapStore {
  comObj = {};
  params = {};
  @observable currentPosition = {};
  @observable initResponsed = false;// 是否已经得到初始化数据的响应
  @observable initSuccessed = false;// 初始化数据是否获取成功
  @observable initFailedMsg = "";   // 初始化处理失败的原因
  @observable buttonProps = { loading: false, disabled: false };
  @observable isGetPosition = false;
  @observable singScope = 0;//为0时不限制范围,单位为米 
  @observable lat1 = 0;
  @observable lng1 = 0;
  @observable currentLat = 0;
  @observable currentLng = 0;
  @action
  doNew = (comObj, params) => {
    this.comObj = comObj;
    this.params = params;
    this.lat1 = params.lat1 == "" ? 0 : parseFloat(params.lat1);
    this.lng1 = params.lng1 == "" ? 0 : parseFloat(params.lng1);
	this.getSingInOutScope();
  }

  @action
  doInit = () => {
    Toast.loading(getLabel('385766', '定位中') + '...', 0);
    getCurrentPosition().then((result) => {
      Toast.hide();
      if (result.status == -1) {
        Toast.fail(getLabel('504565', '无法获取定位信息'));
        this.initSuccessed = false;
        this.isGetPosition = false;
      } else {
        this.isGetPosition = true;
        this.currentPosition = result;
      }
      this.initResponsed = true;
      this.initSuccessed = true;
    });
  }

  @action
  doClear = () => {
    this.currentPosition = {};
    this.initResponsed = false;
    this.initSuccessed = false;
    this.initFailedMsg = "";
  }

  @action
  doSignOut = (typeName, contactWay) => {
    if (!this.isGetPosition) {
      Toast.fail(getLabel('515090', '未能获取定位，签到失败！'));
      return false;
    }
    const planName = typeName + ":" + this.currentPosition.address;
    const params = { customerId: this.params.customerId, planName: planName, lngCurrent: this.currentPosition.position[0], latCurrent: this.currentPosition.position[1], contactWay: contactWay };
    this.buttonProps = { loading: true, disabled: true };
    this.currentLat = this.currentPosition.position[1];
    this.currentLng = this.currentPosition.position[0];
    if (this.checkSignScope() == 0) {
      Apis.signInOut(params).then(data => {
        const status = data.status;
        if (status == 1) {
          Bubble.success(getLabel('83551', "保存成功"), 1, { backgroundColor: adapter.BUBBLE_BGCOLOR }, 2, () => {
            this.comObj.props.history.go(-1);
            if (this.comObj.props.cardStore) {
              this.comObj.props.cardStore.toContactTab();
            }
            this.buttonProps = { loading: false, disabled: false };
          });
        } else {
          Toast.fail(data.errorMsg);
          this.buttonProps = { loading: false, disabled: false };
        }
      });
    } else if (this.checkSignScope() == 1) {
      Toast.fail(getLabel('515085', '客户经纬度有误，请检查客户经纬度！'));
      this.buttonProps = { loading: false, disabled: false };
    } else if (this.checkSignScope() == 2) {
      Toast.fail(contactWay == 3 ? getLabel('515481', '不在签到范围内！') : getLabel('515482', '不在签退范围内！'));
      this.buttonProps = { loading: false, disabled: false };
    } else if (this.checkSignScope() == 3) {
      Toast.fail(getLabel('517356', '请在客户卡片中维护地址'));
      this.buttonProps = { loading: false, disabled: false };
    }
  }
  @action
  getSingInOutScope = () => {
    Tools.callApi({ url: '/api/crm/mobile/signInAndOut', method: 'GET', params: { option: "get" } }).then(result => {
      this.singScope = result.scope;
    });
  }
  @action
  checkSignScope = () => {
    let distance = this.getDistance();
    // console.log(distance);
    // Toast.fail(distance);
    if (this.singScope != 0) {
     if (this.lat1 == 0 || this.lng1 == 0 || Number.isNaN(this.lat1) || Number.isNaN(this.lng1)) {
        return 3;
      }
      if ("NaN" == distance) { 
        return 1;
      }
      if (distance > this.singScope) {//实际计算的距离大于规定签到的距离
        return 2;
      }
    }
    return 0;
  }
  /**
  * @returns {Number}
  */
  getDistance = () => {
    // console.log("实际经度---：" + this.lng1);
    // console.log("实际纬度---：" + this.lat1);
    // console.log("实际经度+++：" + this.currentLng);
    // console.log("实际纬度+++：" + this.currentLat);
    // console.log("this.lat1 + this.currentLat", this.lat1 + this.currentLat);
    var f = this.getRad((this.lat1 + this.currentLat) / 2);
    var g = this.getRad((this.lat1 - this.currentLat) / 2);
    var l = this.getRad((this.lng1 - this.currentLng) / 2);
    // console.log("f", f)
    // console.log("g", g)
    // console.log("l", l)
    var sg = Math.sin(g);
    var sl = Math.sin(l);
    var sf = Math.sin(f);
    var s, c, w, r, d, h1, h2;
    var a = 6378137.0;//The Radius of eath in meter.   
    var fl = 1 / 298.257;
    sg = sg * sg;
    sl = sl * sl;
    sf = sf * sf;
    s = sg * (1 - sl) + (1 - sf) * sl;
    c = (1 - sg) * (1 - sl) + sf * sl;
    w = Math.atan(Math.sqrt(s / c));
    r = Math.sqrt(s * c) / w;
    d = 2 * w * a;
    h1 = (3 * r - 1) / 2 / c;
    h2 = (3 * r + 1) / 2 / s;
    s = d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg));
    // s = s / 1000;
    s = parseFloat(s.toFixed(2));//指定小数点后的位数。   
    return s;
  }

  getRad = (d) => {
    var PI = Math.PI;
    return d * PI / 180.0;
  }
}